package com.yunupmall.service.ford.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunupmall.api.fordVO.EvaluateVO;
import com.yunupmall.api.fordentity.Evaluate;
import com.yunupmall.api.fordentity.ExpandOrder;
import com.yunupmall.api.fordentity.Order;
import com.yunupmall.api.fordentity.Sales;
import com.yunupmall.api.po.OrderCounts;
import com.yunupmall.entity.admin.memberpojo.Member;
import com.yunupmall.mapper.FordMapper.EvaluateMapper;
import com.yunupmall.service.ford.service.EvaluateService;
import com.yunupmall.service.ford.service.ExpandOrderService;
import com.yunupmall.service.ford.service.OrderService;
import com.yunupmall.service.ford.service.SaleService;
import com.yunupmall.uits.SessionUtis;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class Evaluateimpl  extends ServiceImpl<EvaluateMapper, Evaluate> implements EvaluateService {
    @Autowired
    private  ExpandOrderService expandOrderService;
    @Autowired
    private  OrderService orderService;
    @Autowired
    private EvaluateMapper evaluateMapper;

    @Autowired
    private SaleService saleService;
    @Override
    public String saveData(EvaluateVO evaluatevo) {
        LambdaQueryWrapper<ExpandOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExpandOrder ::getOrderId,evaluatevo.getOrderId());
        wrapper.eq(ExpandOrder ::getDeflag,0);
        List<ExpandOrder> list = expandOrderService.list(wrapper);
        if (ObjectUtil.isEmpty(evaluatevo.getFalg())){
            Member member =(Member)SessionUtis.getSessionAttribute("member");
            evaluatevo.setMId(member.getId());
            evaluatevo.setMName(member.getName());
        }
        Order order = new Order();
        order.setId(evaluatevo.getOrderId());
        order.setOrderStatic(4);
        orderService.updateById(order);
        //商品id
       List<Evaluate> evaluateList = new ArrayList<>();
        List<Integer> commtityId = list.stream().map(ExpandOrder::getCId).collect(Collectors.toList());
        commtityId.forEach(id->{
            Evaluate evaluate = new Evaluate();
            BeanUtils.copyProperties(evaluatevo,evaluate);
            evaluate.setCommtityId(id);
            evaluate.setCeaterTime(LocalDateTime.now());
            evaluate.setDeflag(0);
            evaluateList.add(evaluate);
        });
        if (this.saveBatch(evaluateList)) {
            return "新增成功";
        }else {
            throw new RuntimeException("失败");
        }
    }

    /**
     * 分页
     * @param evaluatevo
     * @return
     */
    @Override
    public Page<Evaluate> pageData(EvaluateVO evaluatevo) {
        LambdaQueryWrapper<Evaluate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Evaluate ::getCommtityId,evaluatevo.getCommtityId());
        wrapper.eq(Evaluate ::getDeflag,0);
        Page<Evaluate> evaluatePage = new Page<>(evaluatevo.getPage(), evaluatevo.getLimit());
        Page<Evaluate> page = this.page(evaluatePage, wrapper);
        return page;
    }

    /**
     * 销量和评价
     * @param cId
     * @return
     */
    @Override
    public OrderCounts cumulative(Integer cId) {
        Integer counts = evaluateMapper.getCounts(cId);
        LambdaQueryWrapper<Sales> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sales ::getCId,cId);
        int saleCounts = 0;
        List<Sales> list = saleService.list(wrapper);
        for (Sales sales : list) {
            saleCounts += sales.getCounts();
        }
        OrderCounts orderCounts = new OrderCounts();
        orderCounts.setMonth(counts);
        orderCounts.setCounts(saleCounts);
        return orderCounts;
    }
}
